<?php

/**
 * Description of offer
 *
 * @author Teis Lindemark
 */

$db = new PGSQLConnector('u_superuser');

$offer = null;

if(isset($_GET['submit']) && isset($_GET['offer'])) {
    $offer = $_GET['offer'];
}

$offersSQL = "SELECT DISTINCT\n";
$offersSQL .= "o.idoffer AS idoffer,\n";
$offersSQL .= "o.offerref AS refno,\n";
$offersSQL .= "org.abbreviation AS abbreviation,\n";
$offersSQL .= "org.name AS name,\n";
$offersSQL .= "org.business AS business,\n";
$offersSQL .= "o.offertype AS offertype,\n";
$offersSQL .= "o.idorg_offeredby AS idorg,\n";
$offersSQL .= "o.durationminweeks AS durationminweeks,\n";
$offersSQL .= "o.durationmaxweeks AS durationmaxweeks,\n";
$offersSQL .= "o.datefrom AS periodfrom,\n";
$offersSQL .= "o.dateuntil AS perioduntil,\n";
$offersSQL .= "o.idiaeste_responsible AS idiaeste,\n";
$offersSQL .= "o.kindofwork AS kindofwork,\n";
$offersSQL .= "o.specialization AS specialization,\n";
$offersSQL .= "o.otherrequirements AS otherreq,\n";
$offersSQL .= "o.studybeginning AS studybeginning,\n";
$offersSQL .= "o.studymiddle AS studymiddle,\n";
$offersSQL .= "o.studyend AS studyend,\n";
$offersSQL .= "o.salaryperperiod AS salaryperperiod,\n";
$offersSQL .= "o.salaryperiod AS salaryperiod,\n";
$offersSQL .= "o.currency AS currency,\n";
$offersSQL .= "o.scholarship AS scholarship,\n";
$offersSQL .= "o.livingandlodgingcost AS livingandlodgingcost,\n";
$offersSQL .= "o.livingandlodgingperiod AS livingandlodgingperiod,\n";
$offersSQL .= "o.otherconditions AS otherconditions,\n";
$offersSQL .= "o.worktype AS worktype,\n";
$offersSQL .= "i.abbreviation AS iaeste,\n";
$offersSQL .= "cn.name AS country,\n";
$offersSQL .= "cn.countrycode AS countrycode\n";
$offersSQL .= "FROM offer AS o\n";
$offersSQL .= "JOIN org AS org ON o.idorg_offeredby = org.idorg\n";
$offersSQL .= "LEFT JOIN org AS i ON o.idiaeste_responsible = i.idorg\n";
$offersSQL .= "JOIN country AS cn ON org.countrycode = cn.countrycode\n";
$offersSQL .= "JOIN memo AS m ON m.idoffer = o.idoffer\n";
$offersSQL .= "WHERE o.idoffer = '" . $offer . "'";
$offers = $db->queryUnique($offersSQL);

//Studylevel
$beginning = $offers['studybeginning'];
$middle = $offers['studymiddle'];
$end = $offers['studyend'];
if($beginning === 'Y') $beginning = 'Beginning';
if($beginning === 'N') $beginning = null;
if($middle === 'Y') $middle = 'Middle';
if($middle === 'N') $middle = null;
if($end === 'Y') $end = 'End';
if($end === 'N') $end = null;
$offers['studylevel'] = $beginning . ' ' . $middle . ' ' . $end;

//Salary
$currency = $offers['currency'];
$salary = $offers['salaryperperiod'];
$salaryperiod = $offers['salaryperiod'];
$converted = currencyConverter($salary, $currency);
$offers['salary'] = $currency . ' ' . $salary . ' each ' . $salaryperiod . ' (' . $converted . ')';

//Scholarship
$offers['scholarship'] = 'NOK ' . $offers['scholarship'];

//Living and loding cost
$converted = currencyConverter($offers['livingandlodgingcost'], $currency);
$offers['livingloding'] = $currency . ' ' . $offers['livingandlodgingcost'] . ' ' . $offers['livingandlodgingperiod'] . ' (' . $converted . ')';

//Work type
$worktype = $offers['worktype'];
if($worktype === 'P') $worktype = 'Professional';
if($worktype === 'D') $worktype = 'Research and development';
if($worktype === 'W') $worktype = 'Working environment';
if($worktype === 'X') $worktype = 'Non specific';
$offers['worktype'] = $worktype; 

//Languages
$offerlanSQL = "SELECT\n";
$offerlanSQL .= "idoffer,\n";
$offerlanSQL .= "language,\n";
$offerlanSQL .= "languagelevel,\n";
$offerlanSQL .= "required\n";
$offerlanSQL .= "FROM offer_language";
$offerlan = $db->query2array($offerlanSQL);

$tmp = Array();
foreach($offerlan as $lan) {
    if($lan['required'] === 'Y') $lan['required'] = 'Required';
    else $lan['required'] = null;
    $tmp[$lan['idoffer']][] = $lan['language'] . '(' . $lan['languagelevel'] . ' ' . $lan['required'] . ')';
}
$offerlan = $tmp;
unset($tmp);

$studyfieldSQL = "SELECT\n";
$studyfieldSQL .= "studyfield,\n";
$studyfieldSQL .= "description\n";
$studyfieldSQL .= "FROM studyfield\n";
$studyfieldSQL .= "WHERE timedeleted IS NULL AND official = 'Y'\n";
$studyfieldSQL .= "ORDER BY studyfield";
$studyfield = $db->query2array($studyfieldSQL);
    

$stuyfieldOfferSQL = "SELECT\n";
$stuyfieldOfferSQL .= "idoffer,\n";
$stuyfieldOfferSQL .= "studyfield\n";
$stuyfieldOfferSQL .= "FROM offer_studyfield\n";
$stuyfieldOfferSQL .= "WHERE idoffer = '" . $offer . "'";
$studyfieldOffer = $db->query2array($stuyfieldOfferSQL);

$tmp = Array();
foreach($studyfieldOffer as $sfo) {
    $tmp[$sfo['idoffer']][] = $sfo['studyfield'];
}
$studyfieldOffer = $tmp;
unset($tmp);

$template = newPage('offer');
$template->title = 'Offers - IAESTE Application System';
$template->jobOffers = $offers;
$template->offerStudyfields = $studyfieldOffer;
$template->l = $l;
$template->language = $offerlan;
//$template->errors = $error;

try {
    echo $template->execute();
} catch (Exception $e) {
    echo $e;
}

?>
